Program onloan_avail_over_episode
      character*6 ticker
      character*8 cusp,cusip
      integer(kind=8):: nshrs,nvalue,jvalue,ivol,numshrs(5500),ntot(5500),navail(5500)
      dimension nep(5500),ntype(5500),jyr(5500),jmn(5500),jdy(5500),vwret(5500),retrn(5500),pctonloan(5500)
      dimension pctavail(5500) 
!      open(unit=10,file='e:\FIS_duration\episode_data_type_total.txt')
!      open(unit=99,file='e:\FIS_duration\onloan_avail_over_episode.txt')
      open(unit=10,file='e:\FIS_duration\episode_data_type_total_rep.txt')
      open(unit=99,file='e:\FIS_duration\onloan_avail_over_episode_rep.txt')
      iend=0
      i=0
1     i=i+1      
      read(10,1000,end=900)nep(i),ntype(i),jast,jyr(i),jmn(i),jdy(i),cusp,ticker,itickt,numshrs(i),age,favg,fmax,fmin,fstd,iperm,price,ivol,retrn(i),ishrout,vwret(i),util,navail(i),ntot(i)
1000  format(1x,i4,1x,i1,1x,i8,1x,i4,2i2,1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,i12) 
      pctonloan(i)=float(numshrs(i))/(1000.*float(ishrout))
      pctavail(i)=float(navail(i))/(1000.*float(ishrout))
      if(nep(i).eq.nep(1)) go to 1
5     icnt=i-1
      if(icnt.le.60) go to 100
      iskip=0
      avgonloan=0.
      avgavail=0.
      onln2040=0.
      avl2040=0.
      onln4160=0.
      avl4160=0.
      onln6180=0.
      avl6180=0.
      onln81100=0.
      avl81100=0.
      onl100120=0.
      avl100120=0.
      do 20 ii=1,20
          avgonloan=avgonloan+pctonloan(ii)*.05
          if(navail(ii).lt.0) iskip=1
          avgavail=avgavail+pctavail(ii)*.05
20    continue          
      do 30 ii=21,40
          onln2040=onln2040+(pctonloan(ii)-avgonloan)*.05
          if(iskip.eq.1) go to 30
          avl2040=avl2040+(pctavail(ii)-avgavail)*.05
30    continue
      do 40 ii=41,60
          onln4160=onln4160+(pctonloan(ii)-avgonloan)*.05
          if(iskip.eq.1) go to 40
          avl4160=avl4160+(pctavail(ii)-avgavail)*.05
40    continue
      if(icnt.lt.80) onln6180=-9.
      if(icnt.lt.80) avl6180=-9.
      if(icnt.lt.80) onln81100=-9.
      if(icnt.lt.80) avl81100=-9.
      if(icnt.lt.80) onl100120=-9.
      if(icnt.lt.80) av100120=-9.
      if(icnt.lt.80) go to 95
      do 45 ii=61,80
          onln6180=onln6180+(pctonloan(ii)-avgonloan)*.05
          if(iskip.eq.1) go to 45
          avl6180=avl6180+(pctavail(ii)-avgavail)*.05
45    continue
      if(icnt.lt.100) onln81100=-9.
      if(icnt.lt.100) avl81100=-9.
      if(icnt.lt.100) onl100120=-9.
      if(icnt.lt.100) av100120=-9.
      if(icnt.lt.100) go to 95
      do 50 ii=81,100
          onln81100=onln81100+(pctonloan(ii)-avgonloan)*.05
          if(iskip.eq.1) go to 50
          avl81100=avl81100+(pctavail(ii)-avgavail)*.05
50    continue
      if(icnt.lt.120) onl100120=-9.
      if(icnt.lt.120) av100120=-9.
      if(icnt.lt.120) go to 95
      do 55 ii=101,120
          onl100120=onl100120+(pctonloan(ii)-avgonloan)*.05
          if(iskip.eq.1) go to 55
          avl100120=avl100120+(pctavail(ii)-avgavail)*.05
55    continue
      
95    if(iskip.eq.1) avl2040=-9.
      if(iskip.eq.1) avl4160=-9.
      if(iskip.eq.1) avl6180=-9.
      if(iskip.eq.1) avl81100=-9.
      if(iskip.eq.1) avl100120=-9.
      if(iskip.eq.1) avgavail=-9.

      write(6,9900)nep(1),icnt,ntype(1),avgonloan,onln2040,onln4160,onln6180,onln81100,onl100120,avgavail,avl2040,avl4160,avl6180,avl81100,avl100120
      write(99,9900)nep(1),icnt,ntype(1),avgonloan,onln2040,onln4160,onln6180,onln81100,onl100120,avgavail,avl2040,avl4160,avl6180,avl81100,avl100120
9900  format(2(1x,i4),1x,i1,2(1x,f9.6,5(1x,f10.5)))      
100   continue
      nep(1)=nep(i)
      ntype(1)=ntype(i)
      jyr(1)=jyr(i)
      jmn(1)=jmn(i)
      jdy(1)=jdy(i)
      retrn(1)=retrn(i)
      vwret(1)=vwret(i)
      ntot(1)=ntot(i)
      numshrs(1)=numshrs(i)
      navail(1)=navail(i)
      i=1
      go to 1
900   continue
      iend=iend+1
      if(iend.eq.1) go to 5
      stop
end program onloan_avail_over_episode